package org.itzheng.myrxframe.http.rxjava;

import android.os.Looper;
import android.util.Log;

import org.itzheng.myrxframe.http.bean.abase.BaseResult;

import java.io.Serializable;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Title:重新写观察者，将回调转成简单的成功与失败<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2019-4-10.
 */
public abstract class MyObserver<T extends Serializable> implements Observer<BaseResult<T>> {
    IRxNet mRxNet;
    private static final String TAG = "MyObserver";

    /**
     * 将生命周期传进来，任务执行完毕立即释放
     *
     * @param iRxNet
     */
    public MyObserver(IRxNet iRxNet) {
        mRxNet = iRxNet;
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (mRxNet != null) {
            mRxNet.addDisposable(this, d);
        }
    }

    /**
     * 判断是否是主线程
     *
     * @return
     */
    public boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    /**
     * 这里的东西基本都是在主线程
     *
     * @param value
     */
    @Override
    public void onNext(final BaseResult<T> value) {
        Log.w(TAG, Thread.currentThread().getName() + " onNext: ");
        //如果有需要耗时操作可以继续Rxjava
        if (false) {
            Observable.create(new ObservableOnSubscribe<BaseResult<T>>() {
                @Override
                public void subscribe(ObservableEmitter<BaseResult<T>> e) throws Exception {
                    Log.w(TAG, "subscribe: " + "我先睡一觉");
                    Thread.sleep(2000);
                    Log.w(TAG, "subscribe: " + "睡醒了");
                    e.onNext(value);
                }
            }).subscribeOn(Schedulers.io())
                    //如果设定到主线程，结束时，还是回到主线程
                    .observeOn(isMainThread() ? AndroidSchedulers.mainThread() : Schedulers.io())
                    .subscribe(new Consumer<BaseResult<T>>() {
                        @Override
                        public void accept(BaseResult<T> tBaseResult) throws Exception {
                            removeDisposable();
                            onSuccess(value.dsERP.rows);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            removeDisposable();
                            onError(throwable.toString());
                        }
                    })
            ;
        } else {
            onSuccess(value.dsERP.rows);
        }
    }

    /**
     * 成功时的回调
     *
     * @param rows
     */
    public abstract void onSuccess(List<T> rows);

    /**
     * 失败时的回调
     *
     * @param error
     */
    public abstract void onError(String error);

    @Override
    public void onError(Throwable e) {
        removeDisposable();
        onError(e.toString());
    }

    @Override
    public void onComplete() {
        removeDisposable();
    }

    /**
     * 结束后，就移除监听，提前是否内存
     */
    private void removeDisposable() {
        if (mRxNet != null) {
            mRxNet.removeDisposable(this);
            mRxNet = null;
        }
    }
}
